//----------------------------------------------------------------------------
// filecmd.cpp
//
// file open and save commands
//
// Copyright (C) 2011 Neil Butterworth
//----------------------------------------------------------------------------

#include <windows.h>
#include <string>
#include <iostream>
#include "command.h"
#include "cmdline.h"
#include "util.h"

using namespace std;

namespace Swine {

//----------------------------------------------------------------------------
// Command & option names
//----------------------------------------------------------------------------

const char * const OPEN_CMD 	= "open";
const char * const SAVE_CMD 	= "save";
const char * const CAPTION_OPT 	= "caption";
const char * const DIR_OPT 		= "dir";

//----------------------------------------------------------------------------
// Help
//----------------------------------------------------------------------------

const char * const OPEN_HELP =
	"get existing file name via open dialog\n"
	"options:\n"
	" --caption     specify message box caption (default is 'Open File') \n"
	" --dir         directory in which to open dialog (default is current\n";

const char * const SAVE_HELP =
	"get file name via save dialog\n"
	"options:\n"
	" --caption     specify message box caption (default is S'ave File') \n"
	" --dir         directory in which to open dialog (default is current\n";

//----------------------------------------------------------------------------
// Command class and registration
//----------------------------------------------------------------------------

class FileCmdBase : public Command {
	public:
		FileCmdBase( const string & name, const string &  help )
						: Command( name, help ) {}

};

class OpenCmd : public FileCmdBase {
	public:
		OpenCmd() : FileCmdBase( OPEN_CMD, OPEN_HELP ) {}
		int Execute( CmdLine &  ) const;
};

class SaveCmd : public FileCmdBase {
	public:
		SaveCmd() : FileCmdBase( SAVE_CMD, SAVE_HELP ) {}
		int Execute( CmdLine &  ) const;
};

static AddCommand <OpenCmd> cmd1_;
static AddCommand <SaveCmd> cmd2_;

//----------------------------------------------------------------------------
// Basically identical functions - need to refactor this
//----------------------------------------------------------------------------

int OpenCmd :: Execute( CmdLine & cl ) const {

	string caption = cl.ExtractOption( CAPTION_OPT, "Open File" );
	string dir = cl.ExtractOption( DIR_OPT, "" );
	cl.MustBeEmpty();
	OPENFILENAME ofs = MakeOFS();
	const int FBSIZE = 1024;
	char buf[ FBSIZE + 1 ] = {0};
	ofs.lpstrFile = buf;
	ofs.nMaxFile = FBSIZE;
	ofs.lpstrTitle = caption.c_str();
	ofs.lpstrInitialDir = dir == "" ? 0 : dir.c_str();
	ofs.Flags = OFN_FILEMUSTEXIST + OFN_PATHMUSTEXIST;
	GetOpenFileName( & ofs );
	Reply( buf );
	return 0;

}

int SaveCmd :: Execute( CmdLine & cl ) const {

	string caption = cl.ExtractOption( CAPTION_OPT, "Save File As" );
	string dir = cl.ExtractOption( DIR_OPT, "" );
	cl.MustBeEmpty();
	OPENFILENAME ofs = MakeOFS();
	const int FBSIZE = 1024;
	char buf[ FBSIZE + 1 ] = {0};
	ofs.lpstrFile = buf;
	ofs.nMaxFile = FBSIZE;
	ofs.lpstrTitle = caption.c_str();
	ofs.lpstrInitialDir = dir == "" ? 0 : dir.c_str();
	GetSaveFileName( & ofs );
	Reply( buf );
	return 0;

}

//----------------------------------------------------------------------------

} // namespace

